package www.wen.com.common.lib.view.popuview

import android.content.Context
import android.view.View
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.VERTICAL
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.lxj.xpopup.core.BottomPopupView
import com.lxj.xpopup.util.XPopupUtils
import www.wen.com.common.R

/**
 * @author WEN
 * @Description:
 * @date 2021/4/25 21:35
 */
abstract class BaseBottomListPopupView<T>(
        context: Context,
        var data: List<T>,
) : BottomPopupView(context),
        OnItemClickListener {

    lateinit var mRvAdapter: BaseQuickAdapter<T, BaseViewHolder>

    private var mRecyclerView: RecyclerView? = null

    override fun onCreate() {
        super.onCreate()
        initView()
        initRvList()
    }

    private fun initView() {
        mRecyclerView = findViewById(R.id.rvList)
        if (mRecyclerView == null) throw IllegalArgumentException("布局资源必须拥有rvList的RecycleView的Id")
    }

    private fun initRvList() {
        mRecyclerView!!.run {
            layoutManager = provideLayoutManager()
            addItemDecoration(provideItemDecoration())
            adapter = provideAdapter().apply {
                mRvAdapter = this
            }
            mRvAdapter.setList(data)
            mRvAdapter.setOnItemClickListener(this@BaseBottomListPopupView)
        }
    }

    /**
     * 提供布局管理器
     */
    open fun provideLayoutManager(): RecyclerView.LayoutManager = LinearLayoutManager(context)

    /**
     * 提供装饰
     */
    open fun provideItemDecoration(): RecyclerView.ItemDecoration = DividerItemDecoration(context, VERTICAL)

    /**
     * 提供列表adapter
     */
    abstract fun provideAdapter(): BaseQuickAdapter<T, BaseViewHolder>

    override fun getMaxHeight(): Int = (XPopupUtils.getScreenHeight(context) * 0.5f).toInt()

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {}

}